odoo.define('x_spreadsheet', function (require) {
    "use strict";

    const core = require('web.core');
    const rpc = require('web.rpc');
    const AbstractAction = require('web.AbstractAction');


    let XSpreadSheet = AbstractAction.extend({
        template: 'XSpreadSheet',

        init: function (parent, action, params) {
            this.title = "Hello X-Spreadsheet"
            this.action_params = action.params
            this.api_controller = action.params.api_controller
            this.sheet_data = {}
            console.log("this.api_controller", this.api_controller)
            return this._super.apply(this, arguments);
        },

        start: function () {
            console.log("XSpreadSheet Running...");
            this._fetchSheetData()
            return this._super.apply(this, arguments);
        },

        /*
        * Common: Fetch Data
        * */
        _fetchSheetData: function () {
            let self = this
            return rpc.query({
                route: self.api_controller,
                params: {param1: "Hello", param2: "X-Spreadsheet"}
            }).then(result => {
                const {code, data, message} = result
                if (code === 1) {
                    self.sheet_data = data
                    console.log(message, data)
                } else {
                    console.error("Fetch Failed")
                }
                x_spreadsheet('#x_spreadsheet').loadData(self.sheet_data).change(data => {
                    console.log("Change", data)
                });
            })
        },

        /*
        * Common: Save Data
        * */
        _saveSheetData: function () {
            let self = this
            // TODO: save data
        },
    });
    core.action_registry.add('x_spreadsheet', XSpreadSheet);
});
